package messages.webServices;

import integration.utils.ParseXML;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Hashtable;

import javax.ejb.PostActivate;
import javax.ejb.Stateless;
import javax.jws.WebMethod;
import javax.jws.WebService;
import javax.naming.InitialContext;

import entities.CommonUtils;
import entities.vo.ENVTVO;
import fachada.Fachada;

@Stateless
@WebService
public class EnvtWSBeanTienda9 {

	protected Fachada 			fachada;
	private InitialContext 		initialContext;

	@SuppressWarnings("unused")
	@PostActivate
	private void init(){
		System.out.println("Iniciando Webservice");
		inicializarContexto();
		System.out.println("Finalizando la inicializacion del webservice: ");		
	}

	private void inicializarContexto() {
		try{
			String naming = "TiendaZARAServidorEAR/FachadaBean/remote";
			Hashtable<String, String> props = new Hashtable<String, String>();
			props.put(InitialContext.INITIAL_CONTEXT_FACTORY,"org.jnp.interfaces.NamingContextFactory");
			//Url completa de ubicacion del servidor de aplicaciones
			props.put(InitialContext.PROVIDER_URL,"jnp://"+CommonUtils.HOST+":1099");
			//props.put(InitialContext.PROVIDER_URL,"jnp://172.16.171.116:1099");
			//Objeto del tipo InitialContext
			initialContext = new InitialContext(props);
			fachada = (Fachada) initialContext.lookup(naming);
		}catch(Exception ex){
			System.out.println(ex.getMessage());
			ex.printStackTrace();
		}
	}


	@WebMethod
	public boolean enviarEnvtTienda9(String msg){
		String salida="";
		Boolean out=false;
		try {
			if(fachada==null){
				inicializarContexto();
			}
			salida = fachada.recibirENVT(getENVT(msg));
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		if(salida.equals("")){
			out=true;
		}
		return out;
	}
	public ENVTVO getENVT(String envtXML) throws FileNotFoundException, IOException {
        ParseXML parse = new ParseXML();
        return parse.getArticulosENVT(envtXML);
    }
}
